package agendadecontato;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ContatoDao {

    static PreparedStatement ps;
    // a conexão com o banco de dados
    private Connection conexao;

    public ContatoDao() throws SQLException {
        this.conexao = new Conexao().getConexao();
    }

    public void adiciona(Contato contato) throws SQLException {
        // prepara a conexão
        String sql = "insert into contato " + "(nome,telefone,celular,email) " + "values (?,?,?,?)";
        try (PreparedStatement stmt = conexao.prepareStatement(sql)) {
            stmt.setString(1, contato.getNome());
            stmt.setString(2, contato.getTelefone());
            stmt.setString(3, contato.getCelular());
            stmt.setString(4, contato.getEmail());

            //Executa o código SQL
            stmt.execute();
            stmt.close();
        }
    }

    public List<Contato> getLista() throws SQLException {
        String sql;
        sql = "select * from contato";
        try (PreparedStatement stmt = this.conexao.prepareStatement(sql)) {
            try (ResultSet rs = stmt.executeQuery()) {
                List<Contato> lista = new ArrayList<>();
                while (rs.next()) {
                    Contato contato = new Contato();
                    contato.setCodigo(Long.valueOf(rs.getString("codigo")));
                    contato.setNome(rs.getString("nome"));
                    contato.setTelefone(rs.getString("telefone"));
                    contato.setCelular(rs.getString("celular"));
                    contato.setEmail(rs.getString("email"));
                    lista.add(contato);
                }
                return lista;
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public List<Contato> getLista(String nome) throws SQLException {
        String sql = "select * from contato where nome like ?";
        try (PreparedStatement stmt = this.conexao.prepareStatement(sql)) {
            stmt.setString(1, nome);
            try (ResultSet rs = stmt.executeQuery()) {
                List<Contato> lista = new ArrayList<>();
                while (rs.next()) {
                    Contato contato = new Contato();
                    contato.setCodigo(Long.valueOf(rs.getString("codigo")));
                    contato.setNome(rs.getString("nome"));
                    contato.setTelefone(rs.getString("telefone"));
                    contato.setCelular(rs.getString("celular"));
                    contato.setEmail(rs.getString("email"));
                    lista.add(contato);
                }
                return lista;
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public void altera(Contato contato) throws SQLException {
        String sql = "update contato set nome = ? , telefone = ? , celular = ? , email = ? where codigo = ?";
        try (PreparedStatement stmt = this.conexao.prepareStatement(sql)) {
            stmt.setString(1, contato.getNome());
            stmt.setString(2, contato.getTelefone());
            stmt.setString(3, contato.getCelular());
            stmt.setString(4, contato.getEmail());
            stmt.setLong(5, contato.getCodigo());
            //executar a query
            stmt.execute();
            stmt.close();
        }
    }

    public void remove(Contato contato) throws SQLException {
        String sql = "delete from contato where codigo = ?";
        try (PreparedStatement stmt = this.conexao.prepareStatement(sql)) {
            stmt.setLong(1, contato.getCodigo());
            //executar a query
            stmt.execute();
            stmt.close();
        }
    }
}
